package com.example.ymm.modules1.controller.sys;

import com.example.ymm.model.CommonResult;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.controller.BaseController;
import com.example.ymm.modules1.entity.my.sys.SysUser;
import com.example.ymm.modules1.entity.my.user.*;
import com.example.ymm.modules1.query.*;
import com.example.ymm.modules1.service.dto.*;
import com.example.ymm.modules1.service.sys.SysUserService;
import com.example.ymm.modules1.service.user.*;
import com.example.ymm.util.DateKit;
import com.example.ymm.util.UtilValidate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hl/sys/office")
@Api(tags = "pc端 任职资格管理")
@Slf4j
public class SysOfficeUserController extends BaseController {

    @Autowired
    OfficeUserService officeUserService;

    @Autowired
    OfficeChangeLogService officeChangeLogService;

    @Autowired
    OfficePostRequestService officePostRequestService;
    @Autowired
    OfficeScoreSettingService officeScoreSettingService;
    @Autowired
    OfficeSatisfactionMainService officeSatisfactionMainService;

    @ApiOperation(value = "后勤人员列表")
    @RequestMapping(value = "/officeUserList", method = RequestMethod.POST)
    public CommonResult officeUserList(@Valid OfficeUserQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<ViewDeptOfficeUser> list = officeUserService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "保存后勤人员")
    @RequestMapping(value = "/saveOfficeUser", method = RequestMethod.POST)
    public CommonResult saveOfficeUser(@RequestBody OfficeUser officeUser) {
        try{
            if(UtilValidate.isEmpty(officeUser.getOfficeUserId())){
                return CommonResult.error("参数缺失!");
            }
            if(UtilValidate.isNotEmpty(officeUser.getFirstManageTime())){
                String s = DateKit.dateFormat(officeUser.getFirstManageTime(), "yyyy-MM-dd 00:00:00");
                Date date = DateKit.dateFormat(s);
                officeUser.setFirstManageTime(new Timestamp(date.getTime()));
//                if(!s.contains("00:00:00")){
//                    String s1 = DateKit.dateFormat(officeUser.getFirstManageTime(), "yyyy-MM-dd 00:00:00");
//                    Date date = DateKit.dateFormat(s1);
//                    officeUser.setFirstManageTime(new Timestamp(date.getTime()));
//                }
            }
            if(UtilValidate.isNotEmpty(officeUser.getFirstWordTime())){
                String s = DateKit.dateFormat(officeUser.getFirstWordTime(), "yyyy-MM-dd 00:00:00");
                Date date = DateKit.dateFormat(s);
                officeUser.setFirstWordTime(new Timestamp(date.getTime()));
//                if(!s.contains("00:00:00")){
//                    String s1 = DateKit.dateFormat(new Date(officeUser.getFirstManageTime().getTime()), "yyyy-MM-dd 00:00:00");
//                    Date date = DateKit.dateFormat(s1);
//                    officeUser.setFirstWordTime(new Timestamp(date.getTime()));
//                }
            }
            if(UtilValidate.isNotEmpty(officeUser.getFirstPostTime())){
                String s = DateKit.dateFormat(officeUser.getFirstPostTime(), "yyyy-MM-dd 00:00:00");
                Date date = DateKit.dateFormat(s);
                officeUser.setFirstPostTime(new Timestamp(date.getTime()));
//                if(!s.contains("00:00:00")){
//                    String s1 = DateKit.dateFormat(new Date(officeUser.getFirstManageTime().getTime()), "yyyy-MM-dd 00:00:00");
//                    Date date = DateKit.dateFormat(s1);
//                    officeUser.setFirstPostTime(new Timestamp(date.getTime()));
//                }
            }
            officeUserService.saveOrUpdate(officeUser);

            if(UtilValidate.isNotEmpty(officeUser.getSatisfactions())){
                officeSatisfactionMainService.saveOrUpdateBatch(officeUser.getSatisfactions());
            }


            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/deleteOfficeUser", method = RequestMethod.POST)
    public CommonResult delete(@ApiParam(value = "数据主键") @RequestParam(required = true) String officeUserId) {
        try{
            OfficeUser officeUser = officeUserService.getById(officeUserId);
            if(UtilValidate.isEmpty(officeUser)){
                return CommonResult.error("参数有误!");
            }
            officeUserService.removeById(officeUserId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "导入人员名单")
    @RequestMapping(value = "/importOfficeUser", method = RequestMethod.POST)
    public CommonResult importOfficeUser(@RequestBody ImportOfficeUserDto dto) {
        try{
            if(UtilValidate.isEmpty(dto.getList())){
                return CommonResult.error("参数有误！");
            }
            List<String> strs = officeUserService.importOfficeUser(dto);
            return CommonResult.success(strs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "导入后勤专业经验")
    @RequestMapping(value = "/importWorkYear", method = RequestMethod.POST)
    public CommonResult importWorkYear(@RequestBody ImportOfficeUserDto dto) {
        try{
            if(UtilValidate.isEmpty(dto.getList())){
                return CommonResult.error("参数有误！");
            }
            List<String> strs = officeUserService.importWorkYear(dto);
            return CommonResult.success(strs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "导入历史业绩")
    @RequestMapping(value = "/importUserSatisfaction", method = RequestMethod.POST)
    public CommonResult importUserSatisfaction(@RequestBody List<OfficeSatisfactionMain> list) {
        try{
            if(UtilValidate.isEmpty(list)){
                return CommonResult.error("参数有误！");
            }
            List<String> strs = officeUserService.importUserSatisfaction(list);
            return CommonResult.success(strs);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }




    @ApiOperation(value = "同步一次后勤人员钉钉信息")
    @RequestMapping(value = "/syncOfficeUser", method = RequestMethod.POST)
    public CommonResult syncOfficeUser() {
        try{
            officeUserService.syncOfficeUser();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "调岗记录")
    @RequestMapping(value = "/officeChangeList", method = RequestMethod.POST)
    public CommonResult officeChangeList(@Valid OfficeChangeLogQueryCriteria criteria) {
        try{
            List<OfficeChangeLog> list = officeChangeLogService.queryAll(criteria);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "职位要求列表")
    @RequestMapping(value = "/postRequestList", method = RequestMethod.POST)
    public CommonResult postRequestList(@Valid OfficePostRequestQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<OfficePostRequest> list = officePostRequestService.list(criteria, ymPage);
            return CommonResult.success(list,"获取成功");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "职位要求-新增")
    @RequestMapping(value = "/postRequestSave", method = RequestMethod.POST)
    public CommonResult postRequestSave(OfficePostRequest postRequest) {
        try{
            if(UtilValidate.isEmpty(postRequest.getRoleName())){
                return CommonResult.error("参数异常！");
            }
            officePostRequestService.saveOrUpdate(postRequest);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "职位要求-删除")
    @RequestMapping(value = "/postRequestDelete", method = RequestMethod.POST)
    public CommonResult postRequestDelete(@RequestParam String postRequestId) {
        try{
            officePostRequestService.removeById(postRequestId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "职位要求-导入")
    @RequestMapping(value = "/postRequestImport", method = RequestMethod.POST)
    public CommonResult postRequestImport(@RequestBody ImportPostRequestDto dto) {
        try{
            officePostRequestService.postRequestImport(dto);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "职位要求-导入 专业相符")
    @RequestMapping(value = "/postSpecialtyImport", method = RequestMethod.POST)
    public CommonResult postSpecialtyImport(@RequestBody ImportPostRequestDto dto) {
        try{
            officePostRequestService.postSpecialtyImport(dto);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "职位要求-下拉")
    @RequestMapping(value = "/postRequestAll", method = RequestMethod.POST)
    public CommonResult postRequestAll() {
        try{
            List<OfficePostRequest> list = officePostRequestService.queryAll(null);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "胜任度评价")
    @RequestMapping(value = "/qualifiedList", method = RequestMethod.POST)
    public CommonResult qualifiedList(OfficeUserQueryCriteria criteria) {
        try{
            List<OfficeQualifiedDto> list = officeUserService.qualifiedList(criteria);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "胜任度评价 简易")
    @RequestMapping(value = "/getQualifiedList", method = RequestMethod.POST)
    public CommonResult getQualifiedList(OfficeUserQueryCriteria criteria, YmPageDto ymPage) {
        try{
            TableList<OfficeQualifiedDto> list = officeUserService.getQualifiedList(criteria,ymPage);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "胜任度设置列表")
    @RequestMapping(value = "/scoreSettingList", method = RequestMethod.POST)
    public CommonResult scoreSettingList(OfficeScoreSettingQueryCriteria criteria) {
        try{
            List<OfficeScoreSetting> officeScoreSettings = officeScoreSettingService.queryAll(criteria);
            return CommonResult.success(officeScoreSettings);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @ApiOperation(value = "胜任度设置 保存")
    @RequestMapping(value = "/scoreSettingSave", method = RequestMethod.POST)
    public CommonResult scoreSettingSave(OfficeScoreSetting scoreSetting) {
        try{
            if(UtilValidate.isEmpty(scoreSetting.getScoreSettingId())){
                return CommonResult.error("参数异常");
            }
            officeScoreSettingService.saveOrUpdate(scoreSetting);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "满意度列表")
    @RequestMapping(value = "/officeSatisfactionList", method = RequestMethod.POST)
    public CommonResult officeSatisfactionList(OfficeSatisfactionMainQueryCriteria criteria) {
        try{
            List<OfficeSatisfactionMain> list = officeSatisfactionMainService.queryAll(criteria);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @ApiOperation(value = "满意度列表")
    @RequestMapping(value = "/officeSatisfactionRemove", method = RequestMethod.POST)
    public CommonResult officeSatisfactionRemove(@RequestParam String satisfactionMainId) {
        try{
            officeSatisfactionMainService.removeById(satisfactionMainId);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

//    @ApiOperation(value = "满意度批量保存")
//    @RequestMapping(value = "/officeSatisfactionSaveBatch", method = RequestMethod.POST)
//    public CommonResult officeSatisfactionSaveBatch(@RequestBody List<OfficeSatisfactionMain> list) {
//        try{
//            officeSatisfactionMainService.saveOrUpdateBatch(list);
//            return CommonResult.success();
//        }catch (Exception e){
//            log.error(e.getMessage(),e);
//            return CommonResult.error(e.getMessage());
//        }
//    }

}
